package com.xiaomaoaixue.web.controller;

import com.xiaomaoaixue.common.core.domain.R;
import com.xiaomaoaixue.common.core.exception.CustomException;
import com.xiaomaoaixue.common.core.web.controller.BaseController;
import com.xiaomaoaixue.common.core.web.domain.AjaxResult;
import com.xiaomaoaixue.common.log.annotation.Log;
import com.xiaomaoaixue.common.log.enums.BusinessType;
import com.xiaomaoaixue.postapi.api.domain.WeUserInfoVo;
import com.xiaomaoaixue.wecom.service.IWeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 通讯录相关客户Controller
 *
 * @author ruoyi
 * @date 2020-08-31
 */
@RestController
@RequestMapping("/wecom/user")
@Api(tags = "通讯录人员接口")
@Slf4j
public class WeUserController extends BaseController {

    @Autowired
    private IWeUserService weUserService;

    /**
     * 同步成员
     *
     * @return
     */
    @ApiOperation("同步成员")
    @Log(title = "同步成员", businessType = BusinessType.OTHER)
    @GetMapping({"/synchWeUser"})
    public AjaxResult synchWeUser() {
        try {
            weUserService.synchWeUser();
        }catch (CustomException e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success();
    }

    @ApiOperation("获取访问用户身份")
    @Log(title = "获取访问用户身份", businessType = BusinessType.OTHER)
    @GetMapping({"/getUserInfo"})
    public R<WeUserInfoVo> getUserInfo(@RequestParam("code") String code) {
        try {
            WeUserInfoVo userInfo = weUserService.getUserInfo(code);
            return R.ok(userInfo);
        }catch (CustomException e){
            log.error("获取访问用户身份失败",e);
            return R.fail("获取访问用户身份失败");
        }

    }

}
